package com.qeegoo.b2bautozityp.adapter;

//┏┓　　　┏┓ 
//┏┛┻━━━┛┻┓ 
//┃　　　　　　　┃ 　 
//┃　　　━　　　┃ 
//┃　┳┛　┗┳　┃ 
//┃　　　　　　　┃ 
//┃　　　┻　　　┃ 
//┃　　　　　　　┃ 
//┗━┓　　　┏━┛ 
//┃　　　┃  神兽保佑　　　　　　　　 
//┃　　　┃  代码无BUG！ 
//┃　　　┗━━━┓ 
//┃　　　　　　　┣┓ 
//┃　　　　　　　┏┛ 
//┗┓┓┏━┳┓┏┛ 
// ┃┫┫　┃┫┫ 
// ┗┻┛　┗┻┛

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.moudle.goods.model.GoodsDetailResult;

import java.util.List;

import base.lib.util.ActivityManager;

/**
 * Createdk on 2017/9/27.
 */

public class ImagePagerAdapter extends PagerAdapter {
    private final List<GoodsDetailResult.GoodsImage> images;
    protected Object convertView;

    public ImagePagerAdapter(List<GoodsDetailResult.GoodsImage> images) {

        this.images = images;
    }

    @Override
    public int getCount() {
        if (images == null)

            return 0;
        return images.size();
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = View.inflate(ActivityManager.getActivity(), R.layout.adapter_image, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.image);
        GoodsDetailResult.GoodsImage im = images.get(position);
        Glide.with(imageView.getContext())
                .load(im.originalImagePath)
                .placeholder(R.mipmap.placeholder)
                .error(R.mipmap.placeholder)
                .into(imageView);
        container.addView(view);
        if (convertView == view) {
            convertView = null;
        }
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        convertView = object;
    }
}
